﻿Public Class FormMain
    Dim timerScreen As Timer '任务步骤
    Dim timerScreen_interval As Integer = 3000 '时间间隔
    Dim objWorkshopCurrent As New clsWorkshopCurrent '生产设定
    Dim objJBGW As New clsJBGW '接驳工位
    Dim objGtPgdMes As New clsGtPgdMes '派工生产计划
    Dim objProdSpec As New clsProdSpec '成品支座规格主档
    Dim objFlbMst As New clsFlbMst '法兰板主档
    Private Sub FormMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized
        connectDB()
        initTimer()
        getWorkshopCurrent()
        getJBGW()
        calculation()
        warning()
    End Sub

    Private Sub connectDB()
        g_szInitFileName = My.Application.Info.DirectoryPath & "\screen.ini"
        g_Server.szDrive = GetIniFile("SERVER", "DRIVE", "PostgreSQL ANSI", g_szInitFileName)
        g_Server.szIPadr = GetIniFile("SERVER", "IP", "", g_szInitFileName)
        g_Server.szDbn = GetIniFile("SERVER", "DBN", "ZHENANSF", g_szInitFileName)
        g_Server.szUid = GetIniFile("SERVER", "UID", "sa", g_szInitFileName)
        g_Server.szPas = GetIniFile("SERVER", "PAS", "sa", g_szInitFileName)
        g_Server.iOver = CInt(GetIniFile("SERVER", "OVER", "-1", g_szInitFileName))

        If False = g_fDBconnect And 0 = g_iDBconnResult Then
            If "" = g_Server.szIPadr Or "0.0.0.0" = g_Server.szIPadr Then
            Else
                g_fDBconnect = True

                'データベース接続
                With g_Server
                    g_iDBconnResult = DBacs.Connect(.szDrive, .szIPadr, .szDbn, .szUid, .szPas, .iOver)
                    If 1 <> g_iDBconnResult Then
                        .szDrive = CStr(IIf("PostgreSQL" = .szDrive, "PostgreSQL ANSI", "PostgreSQL"))
                        g_iDBconnResult = DBacs.Connect(.szDrive, .szIPadr, .szDbn, .szUid, .szPas, .iOver)
                    End If
                End With
            End If
        End If
    End Sub
    Private Sub initTimer()
        timerScreen = New Timer
        timerScreen.Interval = timerScreen_interval
        AddHandler timerScreen.Tick, AddressOf timerScreen_Tick
        timerScreen.Start()
    End Sub

    Private Sub timerScreen_Tick(sender As Object, e As EventArgs)
        Try
            getWorkshopCurrent()
            getJBGW()
            calculation()
            warning()
        Catch ex As Exception
            timerScreen.Stop()
            MsgBox("与服务器断开连接，请检查网络状态。")
            End 'Application.Restart()
        End Try
    End Sub
    Private Sub getWorkshopCurrent()
        Dim dtWorkshopCurrent As DataTable = objWorkshopCurrent.GetList()
        Dim drFlb As DataRow() = dtWorkshopCurrent.Select("workshop = 1 and current_section = 'FLBZP'")
        If drFlb.Length > 0 Then '法兰板3工位
            Dim pgdId As String = drFlb(0)("b_pgd_sys_id")
            Dim dtGtPgdMes As DataTable = objGtPgdMes.getFlbList(pgdId)
            If dtGtPgdMes.Rows(0)("mst_value").Equals(DBNull.Value) Then
                lbl3gongwei.Text = String.Empty
            Else
                lbl3gongwei.Text = dtGtPgdMes.Rows(0)("mst_value")
            End If
            If lbl3gongwei.Text.Contains("500") Or lbl3gongwei.Text.Contains("600") Then
                lbl_FlbZP_DL.Text = "小料框"
            End If
            If lbl3gongwei.Text.Contains("700") Or lbl3gongwei.Text.Contains("800") Then
                lbl_FlbZP_DL.Text = "中料框"
            End If
            If lbl3gongwei.Text.Contains("900") Or lbl3gongwei.Text.Contains("1000") Then
                lbl_FlbZP_DL.Text = "大料框"
            End If
            If dtGtPgdMes.Rows(0)("count_up").Equals(DBNull.Value) Then
                lblCountUpFlb.Text = "1"
            Else
                lblCountUpFlb.Text = dtGtPgdMes.Rows(0)("count_up")
            End If
            If dtGtPgdMes.Rows(0)("count_down").Equals(DBNull.Value) Then
                lblCountDownFlb.Text = "1"
            Else
                lblCountDownFlb.Text = dtGtPgdMes.Rows(0)("count_down")
            End If
            lblPlanCountFlb.Text = drFlb(0)("plan_quantity") '计划数
        End If
        Dim drGjb As DataRow() = dtWorkshopCurrent.Select("workshop = 1 and current_section = 'GJBZP'")
        If drGjb.Length > 0 Then
            Dim pgdId As String = drGjb(0)("b_pgd_sys_id")
            Dim dtGtPgdMes As DataTable = objGtPgdMes.getGjbList(pgdId)
            lbl6gongwei.Text = dtGtPgdMes.Rows(0)("type_value")
            lblDesignCount.Text = dtGtPgdMes.Rows(0)("gjb_layer_num")
            If lbl6gongwei.Text.Contains("500") Or lbl6gongwei.Text.Contains("600") Then
                lbl_GjbZP_DL.Text = "小料框"
            End If
            If lbl6gongwei.Text.Contains("700") Or lbl6gongwei.Text.Contains("800") Then
                lbl_GjbZP_DL.Text = "中料框"
            End If
            If lbl6gongwei.Text.Contains("900") Or lbl6gongwei.Text.Contains("1000") Then
                lbl_GjbZP_DL.Text = "大料框"
            End If
            lblPlanCountGjb.Text = drGjb(0)("plan_quantity") '计划数
        End If
        Dim drYzz As DataRow() = dtWorkshopCurrent.Select("workshop = 0 and current_section = 'YZZ'")
        If drYzz.Length > 0 Then
            Dim pgdId As String = drYzz(0)("b_pgd_sys_id")
            Dim prodId As String = drYzz(0)("support_model")
            Dim dtProdSpec As DataTable = objProdSpec.selectAllbySpecId(prodId)
            Dim plan_count As String = drYzz(0)("plan_quantity")
            lblPlanCountYZZ.Text = plan_count
            lblProdSpec.Text = dtProdSpec.Rows(0)("mst_name")
            lbl_FLB.Text = dtProdSpec.Rows(0)("flb_type")
            Dim lkSize As String = dtProdSpec.Rows(0)("flb_type")
            If lkSize.Contains("500") Or lkSize.Contains("600") Then
                lbl_Flb_DL.Text = "小料框"
            End If
            If lkSize.Contains("700") Or lkSize.Contains("800") Then
                lbl_Flb_DL.Text = "中料框"
            End If
            If lkSize.Contains("900") Or lkSize.Contains("1000") Then
                lbl_Flb_DL.Text = "大料框"
            End If

            Dim dtFlbMst As DataTable = objFlbMst.getList(dtProdSpec.Rows(0)("flb_type"))
            lblCountDownFlb1.Text = dtFlbMst.Rows(0)("count_down")
            Dim lkSize1 As String = dtProdSpec.Rows(0)("gjb_type")
            If lkSize1.Contains("500") Or lkSize1.Contains("600") Then
                lbl_Gjb_DL.Text = "小料框"
            End If
            If lkSize1.Contains("700") Or lkSize1.Contains("800") Then
                lbl_Gjb_DL.Text = "中料框"
            End If
            If lkSize1.Contains("900") Or lkSize1.Contains("1000") Then
                lbl_Gjb_DL.Text = "大料框"
            End If
            lbl_GJB.Text = dtProdSpec.Rows(0)("gjb_type")
            lblNeedCountGjb1.Text = plan_count
        End If
    End Sub
    Private Sub getJBGW()
        Dim dtJBGW As DataTable = objJBGW.GetList()
        Dim drFlbZp As DataRow() = dtJBGW.Select("gwname = '1FKS-0'")
        If drFlbZp.Length > 0 Then
            lblJBGW3Total.Text = drFlbZp(0)("total") '接驳工位累计总数
            lblJBGW3kTotal.Text = drFlbZp(0)("gwlknum") '当前轨道上的数量
        End If
        Dim drFlb As DataRow() = dtJBGW.Select("gwname = '1FMS'")
        If drFlb.Length > 0 Then
            lblJBGWTotalFlb.Text = drFlbZp(0)("total") '接驳工位累计总数
            lblJBGWmTotalFlb.Text = drFlb(0)("gwlknum") '当前轨道上的数量
        End If
        Dim drGjbZp As DataRow() = dtJBGW.Select("gwname = '1GKX-1'")
        If drGjbZp.Length > 0 Then
            lblJBGW6Total.Text = drGjbZp(0)("total") '接驳工位累计总数
            lblJBGW6kTotal.Text = drGjbZp(0)("gwlknum") '当前轨道上的数量
            Dim drGjbZpMs1_1 As DataRow() = dtJBGW.Select("gwname = '1GMS1-1'")
            Dim drGjbZpMs1_2 As DataRow() = dtJBGW.Select("gwname = '1GMS1-2'")
            'Label47.Text = CInt(drGjbZpMs1_1(0)("total")) + CInt(drGjbZpMs1_2(0)("total")) '接驳工位累计总数
        End If
        Dim drGjb As DataRow() = dtJBGW.Select("gwname = '1GMS-3'")
        If drGjb.Length > 0 Then
            lblJBGWTotalGjb.Text = drFlbZp(0)("total") '接驳工位累计总数
            lblJBGWmTotalGjb.Text = drFlb(0)("gwlknum") '当前轨道上的数量
        End If
    End Sub
    ''' <summary>
    ''' 数量计算
    ''' </summary>
    Private Sub calculation()
        '法兰板装配
        Dim nnn = CInt(lblPlanCountFlb.Text) / 2
        Dim yyy = CDbl(nnn)
        lblPlanCountDownFlb.Text = Math.Ceiling(yyy)
        Dim mmm = CInt(lblPlanCountFlb.Text) - CInt(lblPlanCountDownFlb.Text)
        Dim xxx = CDbl(mmm)
        lblPlanCountUpFlb.Text = Math.Ceiling(xxx)
        lblNeedCountFlb.Text = Math.Ceiling(CInt(lblPlanCountDownFlb.Text) / CInt(lblCountDownFlb.Text)) + Math.Ceiling(CInt(lblPlanCountUpFlb.Text) / CInt(lblCountUpFlb.Text))
        lbl_FlbZP_Count.Text = CInt(lblNeedCountFlb.Text) - CInt(lblJBGW3Total.Text)
        '法兰板接驳工位
        Dim asd = CInt(lblPlanCountYZZ.Text) / CInt(lblCountDownFlb1.Text)
        Dim asd1 = CDbl(asd)
        lblNeedCountFlb1.Text = Math.Ceiling(asd1)
        lbl_Flb_Count.Text = CInt(lblNeedCountFlb1.Text) - CInt(lblJBGWTotalFlb.Text)
        '骨架板装配
        Dim abc = CInt(lblPlanCountGjb.Text) / CInt(lblDesignCount.Text)
        Dim abc1 = CDbl(abc)
        lblNeedCountGjb.Text = Math.Ceiling(abc1)
        lbl_GjbZP_Count.Text = CInt(lblNeedCountGjb.Text) - CInt(lblJBGW6Total.Text)
        '骨架板接驳工位
        lbl_Gjb_Count.Text = CInt(lblNeedCountGjb1.Text) - CInt(lblJBGWTotalGjb.Text)
    End Sub

    Private Sub warning()
        If lblNeedCountFlb.Text.Equals("0") = False Then
            If lbl_FlbZP_Count.Text.Equals("0") = False Then
                Dim xxz1x As Integer = CInt(lblJBGW3kTotal.Text)
                If xxz1x <= 1 Then
                    lanFlb3.LanternBackground = Color.Red
                Else
                    lanFlb3.LanternBackground = Color.Yellow
                End If
            Else
                lanFlb3.LanternBackground = Color.Blue
            End If
        Else
            lanFlb3.LanternBackground = Color.Gray
        End If
        If lblNeedCountFlb1.Text.Equals("0") = False Then
            If lbl_Flb_Count.Text.Equals("0") = False Then
                Dim xxz1 As Integer = CInt(lblJBGWmTotalFlb.Text)
                If xxz1 <= 1 Then
                    lanJBGWFlb.LanternBackground = Color.Red
                Else
                    lanJBGWFlb.LanternBackground = Color.Yellow
                End If
            Else
                lanJBGWFlb.LanternBackground = Color.Blue
            End If
        Else
            lanJBGWFlb.LanternBackground = Color.Gray
        End If
        If lblNeedCountGjb.Text.Equals("0") = False Then
            If lbl_GjbZP_Count.Text.Equals("0") = False Then
                Dim xxz2 As Integer = CInt(lblJBGW6kTotal.Text)
                If xxz2 <= 1 Then
                    lanFlb6.LanternBackground = Color.Red
                Else
                    lanFlb6.LanternBackground = Color.Yellow
                End If
            Else
                lanFlb6.LanternBackground = Color.Blue
            End If
        Else
            lanFlb6.LanternBackground = Color.Gray
        End If
        If lblNeedCountGjb1.Text.Equals("0") = False Then
            If lbl_Gjb_Count.Text.Equals("0") = False Then
                Dim xxz2 As Integer = CInt(lblJBGWmTotalGjb.Text)
                If xxz2 <= 1 Then
                    lanJBGWGjb.LanternBackground = Color.Red
                Else
                    lanJBGWGjb.LanternBackground = Color.Yellow
                End If
            Else
                lanJBGWGjb.LanternBackground = Color.Blue
            End If
        Else
            lanJBGWGjb.LanternBackground = Color.Gray
        End If
    End Sub
    Private Sub FormMain_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
        DBacs.Disconnect()
    End Sub
End Class
